package com.yznote.compileserver.oss;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.LinkedList;
import java.util.Queue;

@Component
public class OssClientPool {
    @Autowired
    OssConfig ossConfig;

    private Queue<OSS> clientQueue;
    public OssClientPool(){
        this.clientQueue = new LinkedList<>();
    }
    public OSS getOssClient(){
        synchronized (clientQueue){
            if(clientQueue.isEmpty()){
                OSS ossClient = new OSSClientBuilder().build(ossConfig.getEndPoint(), ossConfig.getAccessId(), ossConfig.getAccessSecret());
                return ossClient;
            }
            return clientQueue.poll();
        }
    }
    public void pushBack(OSS oss){
        synchronized (clientQueue){
            if(clientQueue.size()>=20){
                oss.shutdown();
                return;
            }
            clientQueue.add(oss);
        }
    }
}
